package cm.server.controller;

import cm.server.dao.areaRepository;
import cm.server.entity.area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/area_people")
@CrossOrigin()
public class areaController {
    /**
     * @description 1)场地人口密度 2)数据格式说明：[{area: xxx; data: [ { time: xxx; num: xxx; density: xxx }] }]
     * @date 2021/4/15
     */
    @Autowired
    private areaRepository arearepository;

    //    查
    @RequestMapping(value = "/getAllByArea", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getAllByArea(Integer area) {
        List<area> areas = new ArrayList<>();
        areas = arearepository.findAllByArea(area);
        return areas;
    }


    @RequestMapping(value = "/getAllByDayAndAreaAndTime", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getAllByDayAndAreaAndTime(Integer day, Integer area, Integer time) {
        /**
         * @author vastStar
         * @description 通过[day, area, time]三个属性查找 唯一一个 结果
         * 已经能够使用List<Map>的格式返回值了
         * @param [day, area, time]
         * @return java.util.Map<java.lang.String, java.lang.Float>
         * @date 2021/4/16 17:24
         */
        List<area> areas = arearepository.findAllByDayAndAreaAndTime(day, area, time);
        List<Map<String, Object>> returnByJson = new ArrayList<>();
        for (cm.server.entity.area value : areas) {
            Map<String, Object> returnMap = new HashMap<>();
            Map<String, Object> hashmap = new HashMap<>();
            returnMap.put("area", value.getArea());
            returnMap.put("day", value.getDay());
            hashmap.put("time", value.getTime());
            hashmap.put("num", value.getNum());
            hashmap.put("density", value.getDensity());
            returnMap.put("data", hashmap);
            returnByJson.add(returnMap);
        }
        return returnByJson;
    }

    @RequestMapping(value = "/getAllByAreaAndDay", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getAllByAreaAndDay(Integer day, Integer area) {
        /**
         * @author vastStar
         * @description 通过 day an area 查找数据并按List<Map>返回
         * @param [day, area]
         * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
         * @date 2021/4/17 23:21
         */
        List<area> areas = arearepository.findAllByAreaAndDay(day, area);
        List<Map<String, Object>> returnByJson = new ArrayList<>();
        for (cm.server.entity.area value : areas) {
            Map<String, Object> returnMap = new HashMap<>();
            Map<String, Object> hashmap = new HashMap<>();
            returnMap.put("area", value.getArea());
            returnMap.put("day", value.getDay());
            hashmap.put("time", value.getTime());
            hashmap.put("num", value.getNum());
            hashmap.put("density", value.getDensity());
            returnMap.put("data", hashmap);
            returnByJson.add(returnMap);
        }
        return returnByJson;
    }

//    @RequestMapping(value = "/getNumByDay", method = RequestMethod.GET)
//    @ResponseBody
//    public List<Map<String, Object>> getNumByDay(Integer day) {
//        /**
//         * @author vastStar
//         * @description 通过 day 查询数据，并在内部通过 day and time 查询数据，以{day,time,data:[area_id:num,...]}的格式返回数据
//         * @param [day]
//         * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
//         * @date 2021/4/19 10:14
//         */
//        List<area> areas = arearepository.findNumByDay(day);
//        List<Map<String, Object>> returnByJson = new ArrayList<>();
//        //使用'import java.util.stream.Collectors;'将原数据以 time 为依据进行分组
//        Map<Integer, List<area>> areaListMap = areas.stream().collect(Collectors.groupingBy(area::getTime,
//                TreeMap::new, Collectors.toList()));
//        //获取Map中的所有key
//        Set<Integer> keySet = areaListMap.keySet();
//        //遍历存放所有key的Set集合(利用了Iterator迭代器)
//        for (int timeKey : keySet) {
//            //得到每一个key
//            //通过 timeKey 获取对应的 已经按 time 分好组的 List<area>
//            List<area> areas2 = areaListMap.get(timeKey);
//            Map<String, Object> returnMap = new HashMap<>();
//            Map<Integer, Integer> hashMap = new HashMap<>();
//            returnMap.put("day", day);
//            returnMap.put("time", timeKey);
//            for (area value : areas2) {
//                //依次将 Map<area_id, num> 放入 hashMap 中
//                hashMap.put(value.getArea(), value.getNum());
//            }
//            returnMap.put("data", hashMap);
//            returnByJson.add(returnMap);
//        }
//        return returnByJson;
//    }

    @RequestMapping(value = "/getByArea", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByArea(Integer area) {
        return arearepository.findByArea(area);
    }

    @RequestMapping(value = "/getByDensity", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByDensity(Float density) {
        return arearepository.findByDensity(density);
    }

    @RequestMapping(value = "/getByTime", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByTime(Integer time) {
        return arearepository.findByTime(time);
    }

    @RequestMapping(value = "/getByNum", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByNum(Integer num) {
        return arearepository.findByNum(num);
    }

//    改（在写）
//    @RequestMapping(value = "/putByDensity", method = RequestMethod.POST)
//    @ResponseBody
//    public void putByDensity(@RequestBody int density) {
//        arearepository.
//    }

    //    增
    @RequestMapping(value = "/addByArea", method = RequestMethod.POST)
    @ResponseBody
    public void addByArea(@RequestBody area obj) {
        arearepository.save(obj);
    }

    //    删
    @RequestMapping(value = "/deleteAllByArea", method = RequestMethod.DELETE)
    @ResponseBody
    @Transactional
    public void deleteAllByArea(int area) {
        arearepository.deleteAllByArea(area);
    }

    @RequestMapping(value = "deleteByArea", method = RequestMethod.DELETE)
    @ResponseBody
    @Transactional
    public void deleteByArea(int area) {
        arearepository.deleteByArea(area);
    }
}
